<?php
/**
 * Created by PhpStorm.
 * User: tom_xia
 * Date: 2018/5/29
 * Time: 15:09
 * 提交审核
 */
require_once __DIR__."/login_filter.php";

use app\cls\tom\Response;
use app\cls\tom\SysResource;
use app\cls\tom\Tools;
use app\model\Video;
use app\model\Music;

if(empty($_POST["material_id"])  || empty($_POST["media_url"])){
    Response::output(-1,"参数错误");
}

$materialId = $_POST["material_id"];
$audioUrl = $_POST["media_url"];
$channelCode = isset($_POST["channel_code"]) ? $_POST["channel_code"] :"";

$db = SysResource::getDB(true);
$videoModel = new Video($db);

$musicModel = new Music();
$musicModel->setDb($db);

$videoInfo = $videoModel->getInfo(["material_id"=>$materialId],"id,material_id,pic,cut_music_url,transcode_no_audio_video_url,video_name,time_length,video_desc");
if(empty($videoInfo)){
    Response::output(-1,"无效的请求");
}
try{
    $uuid = Tools::UUID();
    $db->insert("user_material",[
        "ori_material_uuid"=>$materialId,
        "user_material_uuid"=>$uuid,
        "video_url"=>$videoInfo["transcode_no_audio_video_url"],
        "music_url"=>$videoInfo["cut_music_url"],
        "music_id"=>0,
        "video_desc"=>$videoInfo["video_desc"],
        "channel_code"=>$channelCode,
        "audio_url"=>$audioUrl,
        "pic"=>$videoInfo["pic"],
        "add_time"=>date("Y-m-d H:i:s"),
        "user_uuid"=>$Auth["user_uuid"],
        "nickname"=>$Auth["nickname"],
        "head_image"=>$Auth["head_image"],
        "time_length"=>$videoInfo["time_length"],
        "video_name"=>$videoInfo["video_name"],
        "is_save"=>1,
    ]);
    $db->update("video",["dub_number"=>"!dub_number+1"],["material_id"=>$materialId]);

    $redis = SysResource::getRedis();
    $dyKeyPrefix = REDIS_PREFIX."material_dy_";
    $dyKey = $dyKeyPrefix.$materialId;
    $redis->zAdd($dyKey,intval(microtime(true)*1000),$Auth["user_uuid"]);
    //动态截断
    $redis->zRemRangeByRank($dyKey,10,-1);
    Response::output(0,"ok",["job_id"=>$uuid]);
}catch (Exception $e){
    Response::output(-1,"提交失败,请重试");
}







